import { createRouter, createWebHashHistory } from 'vue-router';
// import test from '../views/index/test.vue';
import index from '../views/index/Home.vue';
import login from '../views/index/Login.vue';
import register from '../views/index/register.vue';
import NotFound from '../views/NotFound.vue';
import market from '../views/content/market.vue';
import trade from '../views/content/trade.vue';
import ipo from '../views/content/ipo.vue';
import otc from '../views/content/otc.vue';
import news from '../views/content/news.vue';
import ContentLayout from '../views/content/index.vue'; // 导入布局组件

const routes = [
  {
    path: '/',
    name: 'index',
    component: index,
    meta: { requiresAuth: true } // 需要登录
  },
  {
    path: '/login',
    name: 'login',
    component: login,
    meta: { requiresAuth: false } // 需要登录
  },
  // {
  //   path: '/test',
  //   name: 'test',
  //   component: test,
  //   meta: { requiresAuth: false } // 需要登录
  // },
  {
    path: '/register',
    name: 'register',
    component: register,
    meta: { requiresAuth: false } // 需要登录
  },
  {
    path: '/content',
    name: 'content',
    component: ContentLayout, // 使用布局组件作为父组件
    redirect: '/content/trade', // 默认重定向到交易页面
    meta: { requiresAuth: true }, // 需要登录
    children: [
      {
        path: 'market',
        name: 'market',
        component: market,
        meta: { title: 'market',nav:true }
      },
      {
        path: 'trade',
        name: 'trade',
        component: trade,
        meta: { title: 'trade',nav:false }
      },
      {
        path: 'news',
        name: 'news',
        component: news,
        meta: { title: 'news',nav:true },
      },
      {
        path: 'ipo',
        name: 'ipo',
        component: ipo,
        meta: { title: 'ipo',nav:true }
      },
      {
        path: 'otc',
        name: 'otc',
        component: otc,
        meta: { title: 'otc',nav:true }
      },
    
    ]
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: NotFound,
    meta: { requiresAuth: true }, 
  }
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

// 全局前置守卫
router.beforeEach((to, from, next) => {
  
  const token = localStorage.getItem('token')
  if(to.meta.requiresAuth){
    if(token){
      next();
    }else{
      next({
        path: '/login',
        query: { redirect: to.fullPath } // 保存原目标路径，登录后可跳转回来
      });
    }
  }else{
    next()
  }
  // console.log('token-----',token)
  // console.log("to====",to)
  // if(token){
   
  // }else{
  //   next("/login");
  // }
  // if (to.meta.requiresAuth && !isAuthenticated()) {
  //   next("/login");
  // } else {
   
  // }
});


export default router;